package com.dd.filter;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.StaleObjectStateException;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate.HibernateOptimisticLockingFailureException;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dd.entity.*;
import com.dd.dao.*;
import com.dd.service.*;
import com.dd.util.SmsGet;
import com.dd.util.SmsSend;

public class SmsGetTask extends TimerTask {
	private ApplicationContext ctx;
	private static final Log logger = LogFactory.getLog(SystemTimerTask.class);
	private static boolean isRunning = false;
	ServletContext application;

	public SmsGetTask(ServletContext sc) {
		application = sc;
		this.ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(sc);
		System.out.println(" SmsGetTask start........................");
	}

	public void run() {
		try {
			if (!isRunning) {
				isRunning = true;
				// 
				int hour = new Date().getHours();
				if (hour < 9 || hour > 20) {

				} else {
					DdSmsDAO dsd = (DdSmsDAO) ctx.getBean("DdSmsDAO");
					SmsGet sg = new SmsGet();
					String result =sg.getResult();
					/*
					 * <?xml version="1.0" encoding="gb2312"?>
							<Response>
							<SendList>
							<SendStates>
							MsgID=722597&@OkCount=1&@ErrorCount=0&@
							</SendStates>
							<SendStates>
							MsgID=722602&@OkCount=0&@ErrorCount=1&@
							</SendStates>
							<SendStates>
							MsgID=722598&@OkCount=1&@ErrorCount=0&@
							</SendStates>
							</SendList>
						</Response>
					 */
					boolean b = true;
					int beg=0;
					while(b){
						beg = result.indexOf("<SendStates>",beg);
						if(beg==-1){
							b=false;
						}else{
							beg=result.indexOf("MsgID=",beg)+6;
							int msgid = Integer.parseInt(result.substring(beg,beg=result.indexOf("&@",beg)));
							DdSms ds = dsd.findByMsgId(msgid);		
							beg=result.indexOf("OkCount=",beg)+8;
							int ok = Integer.parseInt(result.substring(beg,beg=result.indexOf("&@",beg)));
							beg=result.indexOf("ErrorCount=",beg)+11;
							int error = Integer.parseInt(result.substring(beg,beg=result.indexOf("&@",beg)));
							if(ds!=null){
								if(ok==1){
									ds.setStatus(3);
									
								}
								if(error==1){
									DdSms dss = new DdSms();
									dss.setCreatedate(new Date());
									dss.setStatus(0);
									dss.setOrderid(ds.getOrderid());
									dss.setOrderstatus(ds.getOrderstatus());
									dss.setPhone(ds.getPhone());
									dss.setSendmsg(ds.getSendmsg());
									dss.setTypes(ds.getTypes());
									dsd.saveOrUpdate(dss);
									ds.setStatus(4);
								}
								dsd.saveOrUpdate(ds);
							}
						}
					}
				}
				isRunning = false;
			} else {
				logger.info("[SmsSendTask]-The last task is not over");
			}
			

		} catch (Exception e) {
			e.printStackTrace();
			this.logger.error(e.toString());
			isRunning = false;
		}

	}
}
